package com.yang.servlet;

import com.yang.pojo.BasicInfo;
import com.yang.pojo.InfoDetail;
import com.yang.service.BasicInfoService;
import com.yang.service.InfoDetailService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/BasicInfoAndDetailListServlet")
public class BasicInfoAndDetailListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BasicInfoService basicInfoService = new BasicInfoService();
        InfoDetailService infoDetailService = new InfoDetailService();

        try {

            ArrayList<BasicInfo> basicInfos = basicInfoService.findBasicInfo();
            for (int i = 0; i < basicInfos.size(); i++) {
                BasicInfo basicInfo = basicInfos.get(i);
                int basicInfoId = basicInfo.getBasicInfoId();
                ArrayList<InfoDetail> infoDetails = infoDetailService.findInfoDetailByBasicId(basicInfoId);
                basicInfo.setInfoDetailList(infoDetails);

            }
            request.setAttribute("basicInfos",basicInfos);
            request.getRequestDispatcher("products_add.jsp").forward(request,response);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
